//
//  KTTopControlBar.m
//  BoWuGuan
//
//  Created by 李峰 on 16/11/2.
//  Copyright © 2016年 KT. All rights reserved.
//

#import "KTTopControlBar.h"
#import "MainConfigure.h"
//#import "KTTopControlBtn.h"
#import "NSAttributedString+SizeRemind.h"

#define KTTopControlBar_baseTag 200
#define KTIndicatorLine_height 1.f
@interface KTTopControlBar ()

@property (nonatomic, strong) UIScrollView *topControlBarScrollView;
@property (nonatomic, strong) NSMutableArray <UIButton *>*buttons;

@property (nonatomic, strong) NSMutableArray *buttonIdMarray;

@property (nonatomic, assign) CGSize textSize;

@property (nonatomic, strong) UIView *indicatorLine;

@end
@implementation KTTopControlBar

+ (instancetype)customeTopControllBar
{
    return [[self alloc]initWithFrame:CGRectMake(SM_SIZE(0), SM_SIZE(0), SM_SCREEN_WIDTH, SM_SIZE(90))];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpTopControlBar];
    }
    return  self;
}

- (void)setUpTopControlBar
{
    
    _topControlBarScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [_topControlBarScrollView setShowsHorizontalScrollIndicator:NO];
    [self addSubview:_topControlBarScrollView];


    _indicatorLine = [UIView new];
    _indicatorLine.backgroundColor = SM_thumeColor;
    [_topControlBarScrollView addSubview:_indicatorLine];

    _buttons = [NSMutableArray array];
    
}

- (void)addButtonInTopControlBarWithName:(NSString *)name
{
    
    [_topControlBarScrollView addSubview:[self buttonFactoryWithName:name]];
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [[[NSAttributedString alloc]initWithString:text attributes:attrs] boundSize:CGSizeMake(999, 333)];
}

- (void) crateTopControlBtn:(NSString *)title
{
    [self addButtonInTopControlBarWithName:title];

    [[_buttons lastObject] setTitleColor:SM_thumeColor forState:UIControlStateNormal];
    for (int i=0; i<_buttons.count-1; i++)
    {
        [_buttons[i] setTitleColor:SM_grayColor forState:UIControlStateNormal];
    }
    [self reloadIndicator];
}

- (void)reloadIndicator
{
    [UIView animateWithDuration:.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^
     {
         _indicatorLine.frame = CGRectMake(0, 0, [_buttons lastObject].width, KTIndicatorLine_height);
         _indicatorLine.center = CGPointMake([_buttons lastObject].centerX,CGRectGetMaxY([_buttons lastObject].frame)+SM_SIZE(-10));
         CGFloat delta = 0;
         if (CGRectGetMaxX([_buttons lastObject].frame) + SM_SIZE(30)>_topControlBarScrollView.width) {
             delta = CGRectGetMaxX([_buttons lastObject].frame) + SM_SIZE(30) - _topControlBarScrollView.width;
         }
         [self.topControlBarScrollView setContentOffset:CGPointMake(delta, 0) animated:NO];
     } completion:^(BOOL finished) {
         _topControlBarScrollView.contentSize = CGSizeMake(CGRectGetMaxX([_buttons lastObject].frame) + SM_SIZE(30), 0);
     }];


}

- (UIButton *)buttonFactoryWithName:(NSString *)name
{
    
    CGFloat delta = SM_SIZE(30);
    CGFloat maxX = !_buttons.count ?: CGRectGetMaxX(_buttons.lastObject.frame);

    _textSize = [self sizeWithText:name font:SM_CFont(32) maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(delta+maxX , 0, _textSize.width, SM_SIZE(80))];
    [button setTitle:name forState:UIControlStateNormal];
    //button.backgroundColor = SM_grayColor;
    [button setTitleColor:SM_darkGrayColor forState:UIControlStateNormal];
    button.titleLabel.font = SM_CFont(32);
    button.tag = _buttons.count + KTTopControlBar_baseTag;
    [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [_buttons addObject:button];

    return button;
}

#pragma mark 代理事件
-(void)btnClicked:(UIButton *)button
{
    NSUInteger index = button.tag - KTTopControlBar_baseTag;

    if (index == self.buttons.count - 1) {
        return;
    }

    if (self.delegate && [self.delegate respondsToSelector:@selector(controlBar:didSelectButtonAtIndex:)]) {
        [self.delegate controlBar:self didSelectButtonAtIndex:index];
    }
    
    if (index != _buttons.count - 1) {
        NSUInteger numOfButton = _buttons.count;
        for (NSUInteger i = index + 1; i <  numOfButton; i ++) {
            UIButton *button = [_topControlBarScrollView viewWithTag:KTTopControlBar_baseTag + i];
            [_buttons removeObject:button];
            [button removeFromSuperview];
        }
        [[_buttons lastObject] setTitleColor:SM_thumeColor forState:UIControlStateNormal];
    }

    [self reloadIndicator];
    
}


@end
